<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
class MatchEventTempResult
{
    private $result1;
    private $result2;
    private $match;

    public function __construct($match)
    {
        $this->match = $match;
        $this->getTempResult();
    }

    public function getTempResult1()
    {
        return $this->result1;
    }

    public function getTempResult2()
    {
        return $this->result2;
    }

    private function getTempResult()
    {
        global $database;

        $this->result1 = 0;
        $this->result2 = 0;

        $team1 = $this->match->match_team1;
        $team2 = $this->match->match_team2;

        $playertable = LM::getLMTable("Playermap");
        $playertable->filterbyProject($this->match->match_project);
        $playertable->filterbyTeam($team1);
        $players[$team1] = $playertable->getRows("playermap_id", 0, 0);

        $playertable = LM::getLMTable("Playermap");
        $playertable->filterbyProject($this->match->match_project);
        $playertable->filterbyTeam($team2);
        $players[$team2] = $playertable->getRows("playermap_id", 0, 0);

        $eventtable = LM::getLMTable("Event");
        $eventtable->filterbyMatch($this->match->match_id);
        $rows = $eventtable->getRows("", 0, 0);

        $eventtype_goal = LM_Eventtypemap::getEventtypebyAssociation("cnt_goals");
        $eventtype_own_goal = LM_Eventtypemap::getEventtypebyAssociation("cnt_own_goals");

        $id1 = $eventtype_goal->getID();
        $id2 = $eventtype_own_goal->getID();

        foreach ($rows AS $row) {

            $event = LM::getLMTable("Event");
            $event->populate($row);

            if ($event->getType() == $id1) {
                if (array_key_exists($event->getPlayermap(), $players[$team1])) {
                    $this->result1++;
                } else if (array_key_exists($event->getPlayermap(), $players[$team2])) {
                    $this->result2++;
                } else {

                }
            } else if ($event->getType() == $id2) {
                if (array_key_exists($event->getPlayermap(), $players[$team1])) {
                    $this->result2++;
                } else if (array_key_exists($event->getPlayermap(), $players[$team2])) {
                    $this->result1++;
                } else {

                }
            }
        }
    }

}